package com.bajiao.admin.controller.store;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSSClient;
import com.bajiao.common.api.vo.Result;
import com.bajiao.common.constant._Whole;
import com.bajiao.common.system.vo.LoginUser;
import com.bajiao.common.util.vans.VansTool;
import com.bajiao.entity.store.Store;
import com.bajiao.service.config.ConfigService;
import com.bajiao.service.store.StoreService;
import com.bajiao.vo.store.StoreAdmin;
import com.bajiao.vo.store.StoreStatistics;
import com.bajiao.vo.store.StoreVerify;
import com.bajiao.vo.user.UserAdmin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.extern.slf4j.Slf4j;
import com.bajiao.admin.base.AdminBaseController;
import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 店铺表
 * @Author: FanJq
 * @Date: 2024-12-12
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "店铺表")
@RestController
@RequestMapping("/store")
public class StoreController extends AdminBaseController<Store, StoreService> {
    @Resource
    private StoreService storeService;
	@Resource
	private ConfigService configService;

    @ApiOperation(value = "店铺表-分页列表查询", notes = "店铺表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(String account, String title, String mobile) {
        startPage();
        List<StoreAdmin> resultList = storeService.getAdminList(account, title, mobile);
        return Result.ok(getDataTable(resultList));
    }

    @ApiOperation(value = "店铺统计列表", notes = "店铺统计列表")
    @GetMapping(value = "/statistics/list")
    public Result<?> getStatisticsList(String title) {
        startPage();
        List<StoreStatistics> resultList = storeService.getStatisticsList(title);
        return Result.ok(getDataTable(resultList));
    }

    @ApiOperation(value = "店铺核销记录", notes = "店铺核销记录")
    @GetMapping(value = "/verify/list")
    public Result<?> getVerifyList(String keywords, String orderNo, String storeTitle, String title, String createTimeBegin, String createTimeEnd) {
        startPage();
        List<StoreVerify> resultList = storeService.getVerifyList(keywords, orderNo, storeTitle, title, createTimeBegin, createTimeEnd);
        return Result.ok(getDataTable(resultList));
    }

    @ApiOperation(value = "店铺表-分页列表查询", notes = "店铺表-分页列表查询")
    @GetMapping(value = "/select")
    public Result<?> getSelect() {
        List<Store> resultList = storeService.lambdaQuery()
                .gt(Store::getStatus, _Whole.ZERO)
                .eq(Store::getDelFlag, _Whole.ZERO)
                .list();
        return Result.ok(resultList);
    }

    /**
     * @param store
     * @return
     * @description: 添加
     * @author: FanJq
     * @date: 2024-12-12
     */
    @ApiOperation(value = "店铺表-添加", notes = "店铺表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody Store store) {
        store.setType(_Whole.TWO);
        storeService.save(store);
        return Result.ok("添加成功！");
    }

    /**
     * @param store
     * @return
     * @description: 编辑
     * @author: FanJq
     * @date: 2024-12-12
     */
    @ApiOperation(value = "店铺表-编辑", notes = "店铺表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody Store store) {
        storeService.updateById(store);
        return Result.ok("编辑成功!");
    }

    /**
     * @param id
     * @return
     * @description: 通过id删除
     * @author: FanJq
     * @date: 2024-12-12
     */
    @ApiOperation(value = "店铺表-通过id删除", notes = "店铺表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        storeService.lambdaUpdate().set(Store::getDelFlag, _Whole.ONE).eq(Store::getId, id).update();
        return Result.ok("删除成功!");
    }

    /**
     * @param ids
     * @return
     * @description: 批量删除
     * @author: FanJq
     * @date: 2024-12-12
     */
    @ApiOperation(value = "店铺表-批量删除", notes = "店铺表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        storeService.lambdaUpdate().set(Store::getDelFlag, _Whole.ONE).in(Store::getId, Arrays.asList(ids.split(","))).update();
        return Result.ok("批量删除成功！");
    }

    /**
     * @param id
     * @return
     * @description: 通过id查询
     * @author: FanJq
     * @date: 2024-12-12
     */
    @ApiOperation(value = "店铺表-通过id查询", notes = "店铺表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        Store store = storeService.getById(id);
        return Result.ok(store);
    }

	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(String account, String title, String mobile, String fileName) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		// Step.2 获取导出数据
		List<StoreAdmin> exportList = storeService.getAdminList(account, title, mobile);
		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, fileName); //此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.CLASS, StoreAdmin.class);
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(fileName, "导出人:" + sysUser.getRealname(), fileName));
		mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		return mv;
	}

    @RequestMapping(value = "/statistics/exportXls")
    public ModelAndView statisticsExportXls(String title, String fileName) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // Step.2 获取导出数据
        List<StoreStatistics> exportList = storeService.getStatisticsList(title);
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, fileName); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, StoreStatistics.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(fileName, "导出人:" + sysUser.getRealname(), fileName));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }

    @RequestMapping(value = "/verify/exportXls")
    public ModelAndView verifyExportXls(String keywords, String orderNo, String storeTitle, String title, String createTimeBegin, String createTimeEnd, String fileName) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // Step.2 获取导出数据
        List<StoreVerify> exportList = storeService.getVerifyList(keywords, orderNo, storeTitle, title, createTimeBegin, createTimeEnd);
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, fileName); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, StoreVerify.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(fileName, "导出人:" + sysUser.getRealname(), fileName));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }

}
